package com.igeek.mygh.TTLQueue.consumer;

import com.igeek.mygh.Sample;
import com.igeek.mygh.TTLQueue.DelayedQueueConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @author xlz;
 * @data 2022-08-09 22:05
 */
@Component
@Slf4j
public class Consumer {
    @Autowired(required = false)
    private Sample sample;
    //监听延时队列，触发短信发送
    @RabbitListener(queues = DelayedQueueConfig.DELAYED_QUEUE_NAME)
    private  void receiveDelayQueue(Message message) throws Exception {
        String msg = new String(message.getBody(),"UTF-8");
        //获取信息中的手机号
        String phone = msg.substring(0,11);
        String trueMsg = msg.substring(11);
        log.info("解析出的手机号={}",phone);
        log.info("解析出的消息={}",trueMsg);
        sample.sendSMS(phone.trim(),trueMsg);
    }
}
